---
title: "ElasticSearch Index Mappings"
date: 2019-8-1
categories:
- elasticsearch
tags:
---

<div id="content">
<div id="table-of-contents">
<h2>Table of Contents</h2>
<div id="text-table-of-contents">
<ul>
<li><a href="#org23dbd08">Types</a>
<ul>
<li><a href="#org1183ae9">text</a>
<ul>
<li><a href="#org1bd611b">参数</a></li>
</ul>
</li>
<li><a href="#orga953ff3">keyword</a></li>
</ul>
</li>
</ul>
</div>
</div>
<div class="org-src-container">
<pre class="src src-json"># PUT my_index 
{
  "mappings": {
    "_doc": { 
      "properties": { 
	"title":    { "type": "text"  }, 
	"name":     { "type": "text"  }, 
	"age":      { "type": "integer" },  
	"created":  {
	  "type":   "date", 
	  "format": "strict_date_optional_time||epoch_millis"
	}
      }
    }
  }
}
</pre>
</div>
<div class="outline-2" id="outline-container-org23dbd08">
<h2 id="org23dbd08">Types</h2>
<div class="outline-text-2" id="text-org23dbd08">
<ul class="org-ul">
<li>简单类型例如 text, keyword, date, long, double, boolean or ip.</li>
<li>支持JSON层级的类型例如 object or nested.</li>
<li>特殊的类型例如 like geo_point, geo_shape, or completion.</li>
</ul>
<ul class="org-ul">
<li>string
<ul class="org-ul">
<li>text and keyword</li>
</ul></li>
<li>Numeric datatypes
<ul class="org-ul">
<li>long, integer, short, byte, double, float, half_float, scaled_float</li>
</ul></li>
<li>Date datatype
<ul class="org-ul">
<li>date</li>
</ul></li>
<li>Boolean datatype
<ul class="org-ul">
<li>boolean</li>
</ul></li>
<li>Binary datatype
<ul class="org-ul">
<li>binary</li>
</ul></li>
<li>Range datatypes
<ul class="org-ul">
<li>integer_range, float_range, long_range, double_range, date_range</li>
</ul></li>
</ul>
<p>
使用fields为字段指定多种索引方式：
</p>
<div class="org-src-container">
<pre class="src src-json">{
  "mappings": {
    "_doc": {
      "properties": {
	"city": {
	  "type": "text",
	  "fields": {
	    "raw": { 
	      "type":  "keyword"
	    }
	  }
	}
      }
    }
  }
}
</pre>
</div>
</div>
<div class="outline-3" id="outline-container-org1183ae9">
<h3 id="org1183ae9">text</h3>
<div class="outline-text-3" id="text-org1183ae9">
<p>
对字段的值进行全文索引，例如邮件内容，商品描述。
这些字段将进行分析（analyzed），即它们索引之前将通过  analyzer 转化成多个独立的词条。
</p>
</div>
<div class="outline-4" id="outline-container-org1bd611b">
<h4 id="org1bd611b">参数</h4>
<div class="outline-text-4" id="text-org1bd611b">
<dl class="org-dl">
<dt>analyzer</dt><dd>词法分析器，用于索引和查询时（除非被search_analyzer重写）</dd>
</dl>
</div>
</div>
</div>
<div class="outline-3" id="outline-container-orga953ff3">
<h3 id="orga953ff3">keyword</h3>
<div class="outline-text-3" id="text-orga953ff3">
<p>
结构化的内容过虑，例如邮件地址，域名，状态码，邮编，标签等。
</p>
<p>
典型的用法是过滤(Find where `status` is 'published')、排序、聚合。keyword只能通过它的精确值匹配。
</p>
</div>
</div>
</div>
</div>
<div class="status" id="postamble">
<p class="date">Date: 2019-8-1</p>
<p class="author">Author: gdme1320</p>
<p class="validation"><a href="http://validator.w3.org/check?uri=referer">Validate</a></p>
</div>
